#!/bin/bash
sudo dnf install keepalived haproxy -y
# ssh vagrant@$MASTER02 "sudo dnf install keepalived haproxy -y"
# ssh vagrant@$MASTER03 "sudo dnf install keepalived haproxy -y"

VIP=192.168.56.110
MASTER01=192.168.56.11
MASTER02=192.168.56.12
MASTER03=192.168.56.13
# read -p "type master node name; (like: 1) " LOCAL_IP
# if [ $LOCAL_IP -eq 1 ]; then
# LOCAL_IP=$MASTER01
# elif [ $LOCAL_IP -eq 2 ]; then
# LOCAL_IP=$MASTER02
# elif [ $LOCAL_IP -eq 3 ]; then
# LOCAL_IP=$MASTER03
# else
# LOCAL_IP=$MASTER01
# fi

LOCAL_IP=$MASTER03
#ip a 命令获取网卡名称
INTERFACE=eth1

cat <<EOF | sudo tee /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend k8s-master
    bind 0.0.0.0:8443
    bind 127.0.0.1:8443
    mode tcp
    option tcplog
    tcp-request inspect-delay 5s
    default_backend k8s-master

backend k8s-master
    mode tcp
    option tcplog
    option tcp-check
    balance roundrobin
    default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
    server k8s-master01    $MASTER01:6443  check
    server k8s-master02    $MASTER02:6443  check
    server k8s-master03    $MASTER03:6443  check
EOF

# 检查haproxy是否存在，否则下掉keepalive
cat <<EOF | sudo tee /etc/keepalived/check_apiserver.sh
#!/bin/bash

err=0
for k in \$(seq 1 3)
do
    check_code=\$(pgrep haproxy)
    if [[ \$check_code == "" ]]; then
        err=\$(expr \$err + 1)
        sleep 1
        continue
    else
        err=0
        break
    fi
done

if [[ \$err != "0" ]]; then
    echo "systemctl stop keepalived"
    /usr/bin/systemctl stop keepalived
    exit 1
else
    exit 0
fi
EOF

sudo chmod +x /etc/keepalived/check_apiserver.sh

sudo cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

cat <<EOF | sudo tee /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id LVS_DEVEL
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5 
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state MASTER
    interface $INTERFACE
    mcast_src_ip $LOCAL_IP
    virtual_router_id 51
    priority 101
    nopreempt
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass K8SHA_KA_AUTH
    }
    virtual_ipaddress {
        $VIP
    }
    track_script {
      chk_apiserver 
    }
}
EOF

sudo systemctl daemon-reload
sudo systemctl enable --now haproxy
sudo systemctl enable --now keepalived

# 测试
# ping 192.168.56.110
# telnet 192.168.56.110 8443
# systemctl status keepalived haproxy
# netstat -lntp | grep 8443
